package com.etc.colldemo;

import java.util.ArrayList;
import java.util.Vector;

/**
 * @author: knowno
 * @create: 2024-09-10 15:14
 * @Version 1.0
 * 1. 当调用add方法的时候，进入ArrayList类的=>public boolean add(E e)
 *  ensureCapacityInternal(size + 1);  // Increments modCount!!
 *         elementData[size++] = e;
 *
 * 2.我们进入ensureCapacityInternal
 * private void ensureCapacityInternal(int minCapacity) {
 *         if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
 *             minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
 *         }
 *
 *         ensureExplicitCapacity(minCapacity);
 *     }
 *
 * 3. 进入  ensureExplicitCapacity(minCapacity);
 *    modCount++;
 *
 *         // overflow-conscious code
 *         if (minCapacity - elementData.length > 0)
 *             grow(minCapacity);
 *
 * 4.在第一次添加到第11个元素的时候，我们要进行扩容,会调用grow方法.
 * 我们debug发现: 10+5=15,因此扩容为原来的1.5倍，后面第二次: 15*1.5=22.
 *
 * private void grow(int minCapacity) {
 *         // overflow-conscious code
 *         int oldCapacity = elementData.length;
 *         int newCapacity = oldCapacity + (oldCapacity >> 1);
 *         if (newCapacity - minCapacity < 0)
 *             newCapacity = minCapacity;
 *         if (newCapacity - MAX_ARRAY_SIZE > 0)
 *             newCapacity = hugeCapacity(minCapacity);
 *         // minCapacity is usually close to size, so this is a win:
 *         elementData = Arrays.copyOf(elementData, newCapacity);
 *     }
 *
 **/
public class TestArrayListCapicity {
    public static void main(String[] args) {

        //我们创建一个循环，向集合中添加30个元素，我们去debug观察其扩容机制=》类内部的源码.
        // debug =>Force Step into

        ArrayList<Object> objects = new ArrayList<>();

        //可以存储任何类型
        for (int i = 0; i < 30; i++) {
            objects.add(i);
        }

        Vector<Object> objects1 = new Vector<>();
        objects1.add("x");


    }
}
